package com.yfp.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.MarCoupon;
import com.yfp.operation.domain.MarCouponPackDetails;
import com.yfp.operation.domain.dto.MarCouponPackCountDTO;
import com.yfp.operation.domain.vo.MarCouponPackDetailVO;
import com.yfp.operation.mapper.MarCouponPackDetailsMapper;
import com.yfp.operation.service.MarCouponPackDetailsService;
import com.yfp.operation.service.MarCouponService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【mar_coupon_pack_details(礼包详情)】的数据库操作Service实现
 * @createDate 2024-09-25 10:37:56
 */
@Service
public class MarCouponPackDetailsServiceImpl extends ServiceImpl<MarCouponPackDetailsMapper, MarCouponPackDetails> implements MarCouponPackDetailsService {

    @Resource
    private MarCouponPackDetailsMapper marCouponPackDetailsMapper;
    @Resource
    private MarCouponService marCouponService;

    @Override
    public List<MarCouponPackCountDTO> countMarCouponByCouponPackIds(List<Long> couponPackIds) {
        if (CollUtil.isEmpty(couponPackIds)) {
            return List.of();
        }
        List<MarCouponPackDetails> marCouponPackDetails = marCouponPackDetailsMapper.selectByCouponPackIds(couponPackIds);
        Map<Long, List<MarCouponPackDetails>> collect = marCouponPackDetails.stream().collect(Collectors.groupingBy(MarCouponPackDetails::getCouponPackId));
        return collect.entrySet().stream().map(item -> {
            List<MarCouponPackDetails> value = item.getValue();
            Long count = value.stream().map(MarCouponPackDetails::getCouponCount).reduce(Long::sum).orElse(null);

            MarCouponPackCountDTO marCouponPackCountDTO = new MarCouponPackCountDTO();
            marCouponPackCountDTO.setCouponPackId(item.getKey());
            marCouponPackCountDTO.setCount(count);
            return marCouponPackCountDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MarCouponPackCountDTO> countByCouponPackIds(List<Long> couponPackIds) {
        if (CollUtil.isEmpty(couponPackIds)) {
            return List.of();
        }
        return marCouponPackDetailsMapper.countByCouponPackIds(couponPackIds);
    }

    @Override
    public List<MarCouponPackDetails> listByCouponPackId(Long couponPackId) {
        return marCouponPackDetailsMapper.selectByCouponPackId(couponPackId);
    }

    @Override
    public List<MarCouponPackDetails> listByCouponPackIds(List<Long> couponPackIds) {
        if (CollUtil.isEmpty(couponPackIds)) {
            return List.of();
        }
        return marCouponPackDetailsMapper.selectByCouponPackIds(couponPackIds);
    }

    @Override
    public List<MarCouponPackDetailVO> complementListData(List<MarCouponPackDetails> marCouponPackDetails) {

        List<Long> couponIds = marCouponPackDetails.stream().map(MarCouponPackDetails::getCouponId).collect(Collectors.toList());
        List<MarCoupon> marCoupons = marCouponService.listByIds(couponIds);
        Map<Long, MarCoupon> marCouponMapById = marCoupons.stream().collect(Collectors.toMap(MarCoupon::getId, item -> item));

        List<MarCouponPackDetailVO> couponPackDetailVOS = marCouponPackDetails.stream().map(item -> {
            MarCoupon marCoupon = marCouponMapById.getOrDefault(item.getCouponId(), new MarCoupon());
            MarCouponPackDetailVO bean = BeanUtil.toBean(marCoupon, MarCouponPackDetailVO.class);
            bean.setCouponCount(item.getCouponCount());
            return bean;
        }).collect(Collectors.toList());

        return couponPackDetailVOS;
    }
}




